package com.huluobo.a2205parkdemo.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.a2205parkdemo.R
import com.huluobo.a2205parkdemo.databinding.ItemNewsBinding
import com.huluobo.base.bean.NewsBeanItem

/**
 *  Created by LC on 2024/9/26.
 */
class NewsPagingAdapter : PagingDataAdapter<NewsBeanItem, NewsPagingAdapter.ViewHolder>(diffUtilCallback) {
    companion object {
        private val diffUtilCallback = object : DiffUtil.ItemCallback<NewsBeanItem>() {
            override fun areItemsTheSame(oldItem: NewsBeanItem, newItem: NewsBeanItem): Boolean {
                return oldItem.newsId == newItem.newsId
            }

            override fun areContentsTheSame(oldItem: NewsBeanItem, newItem: NewsBeanItem): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding: ItemNewsBinding? = DataBindingUtil.bind(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Log.i("TagA","item:${getItem(position).toString()}")
        holder.binding?.itemNewsTitleTv?.text = getItem(position)?.newsTitle
        holder.binding?.itemNewsValueTv?.text = getItem(position)?.newsValue
        holder.binding?.itemNewsTimeTv?.text = getItem(position)?.newsTime
        when (getItem(position)?.tagId) {
            0 -> holder.binding?.itemNewsTagTv?.text = "物业新闻"
            1 -> holder.binding?.itemNewsTagTv?.text = "娱乐新闻"
            2 -> holder.binding?.itemNewsTagTv?.text = "体育新闻"
            3 -> holder.binding?.itemNewsTagTv?.text = "社会新闻"
            4 -> holder.binding?.itemNewsTagTv?.text = "早间新闻"
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_news, parent, false)
        return ViewHolder(view)
    }
}